package copyengine.utils.tick.node
{
	import copyengine.utils.GeneralUtils;
	import copyengine.utils.cache.object.ICEMovieClip;

	import flash.display.DisplayObjectContainer;

	public final class AnimationCacheMovieclipTickObject extends AnimationTickObjectNodeBasic
	{
		private var isCacheTargetPlayFinish:Boolean;

		public function AnimationCacheMovieclipTickObject(_target:DisplayObjectContainer, _callBackFunction:Function, _repeatTime:int, _endAction:int, _mcTotalFrame:int)
		{
			super(_target, _callBackFunction, _repeatTime, _endAction, _mcTotalFrame);
		}

		override protected function doStartTick():void
		{
			isCacheTargetPlayFinish = false;
			cacheTarget.addMovieClipPlayFinishedCallBackFunction(onCacheTargetPlayFinish);
		}

		override protected function doDispose():void
		{
		}

		override protected function doTickTarget(_currentPlayFrame:int):Boolean
		{
			return isCacheTargetPlayFinish;
		}

		override protected function getTargetTotalFrame():int
		{
			return cacheTarget.totalFrames;
		}

		//============================================//
		//==Private  Function
		//============================================//

		private function onCacheTargetPlayFinish():void
		{
			isCacheTargetPlayFinish = true;
			//在播放结束后立即操作Target而不是等到dispose时候才操作
			//因为doDispose会在下帧才调用，因为使用了GlobalTick，
			//像END_ACTION_STOP这种EndAction对帧敏感 所以不能等到doDispose时候才操作
			switch (endAction)
			{
				case END_ACTION_DO_NOTHING:
					break;
				case END_ACTION_REMOVE:
					GeneralUtils.removeTargetFromParent(target);
					break;
				case END_ACTION_STOP:
					cacheTarget.mcPause();
					break;
				case END_ACTION_INVISIBLE:
					cacheTarget.mcPause();
					target.visible = false;
					break;
			}
		}

		private function get cacheTarget():ICEMovieClip
		{
			return target as ICEMovieClip;
		}



	}
}
